---
title: "Schäfer_BJPS_Data_preparation_BES"
author: "Armin Schäfer, WWU"
date: |
  `r lubridate::today()`
output: html_document
---

In this file, I use data from the British Election Study: Fieldhouse, E., J. Green, G. Evans, J. Mellon & C. Prosser (2020): British Election Study Internet Panel Waves 1-19. After registration, it is available [here](https://www.britishelectionstudy.com/data-objects/panel-study-data/). To run the script, you have to download the data first.

The results reported in the paper can be reproduced if you run this script first and then "an_bes.Rmd".

*Note*: I rely on the [R package management tool `pacman`](). Please run `install.packages("pacman")` to install the package from CRAN. The command `pacman::p_load()` will then import all specified packages and install them first in case they are currently not installed on your machine.

```{r, message=FALSE, warning=FALSE}

if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, haven, ggpubr, here,
               janitor, ggdist, patchwork)

tabl <- function(...) table(..., useNA='ifany')

```

Load and recode data.
```{r load data}

# Load the data from your folder

bes <- readRDS(here("raw-data", "bes9_10.rds")) 

bes$ref <- ifelse(bes$euRefVoteW9>1,NA,bes$euRefVoteW9)

bes$yearbrn <- 2016 - as.numeric(bes$ageW9)

bes <- bes %>%
  mutate(cohort = case_when(
    yearbrn %in% 1900:1945 ~ "1",
    yearbrn %in% 1946:1964 ~ "2",
    yearbrn %in% 1965:1979 ~ "3",
    yearbrn %in% 1980:1998 ~ "4"
  ))

bes$cohort <- factor(bes$cohort,
                     levels = c(1:4),
                     labels = c("Interwar",
                                "Baby Boomers",
                                "Generation X",
                                "Millennials"))

bes$male <- ifelse(bes$gender==2,0,bes$gender)

bes <- bes %>%
  mutate_at(vars(populism1W7:populism6W7), ~ifelse(. ==9999,NA,.))

bes$populism <- scales::rescale(bes$populism1W7 +
                                bes$populism2W7 +
                                bes$populism4W7 +
                                bes$populism5W7 +
                                bes$populism6W7, na.rm=T, to=c(0,100))

bes$populism2 <- (bes$populism1W7 +
                                bes$populism2W7 +
                                bes$populism4W7 +
                                bes$populism5W7 +
                                bes$populism6W7)


bes <- bes %>%
    mutate(graduate = case_when(
      anyUniW9 %in% 1:3 ~"1",
      anyUniW9 %in% 0 ~"0"))

arb_bes <- bes %>%
  select(id, cohort, populism, populism2, al_scaleW7_W9)

# Save file to an appropriate folder
saveRDS(arb_bes, file = (here("processed-data", "bes.rds")))

```
